package com.shelly.memento;

import java.io.*;

/**
 * @author: Shelly
 * @create: 2023-01-13 10:08:36
 * @describe:
 */
public class TankFrame {
    private Tank mytank;
    private  Bullet bullet;

    public TankFrame(Tank mytank, Bullet bullet) {
        this.mytank = mytank;
        this.bullet = bullet;
    }

    public void setMytank(Tank mytank) {
        this.mytank = mytank;
    }

    public void setBullet(Bullet bullet) {
        this.bullet = bullet;
    }

    @Override
    public String toString() {
        return "TankFrame{" +
                "mytank=" + mytank +
                ", bullet=" + bullet +
                '}';
    }

    // 保存快照
    public void save(){
        File f = new File("f:/马士兵学习Java/tank.data");
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(f));
            oos.writeObject(mytank);
            oos.writeObject(bullet);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (oos != null){
                try {
                    oos.close();// 关闭文件流
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 读取快照
    public void load(){
        File f = new File("f:/马士兵学习Java/tank.data");
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(f));
            mytank = (Tank) ois.readObject();// 先写的就先读
            bullet = (Bullet) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
